package com.raincat.multimediapicker.utils;

import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.GridView;

/**
 * Created by Administrator on 2018/3/23 0023.
 */

public class NoScrollGridView extends GridView {
    private OnBlankItemClickListener mTouchBlankPosListener;

    public NoScrollGridView(Context context) {
        super(context);
    }

    public NoScrollGridView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST);
        super.onMeasure(widthMeasureSpec, expandSpec);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (!isEnabled())
            return isClickable() || isLongClickable();

        if (event.getActionMasked() == MotionEvent.ACTION_UP) {
            int motionPosition = pointToPosition((int) event.getX(), (int) event.getY());
            if (motionPosition == INVALID_POSITION) {
                if (mTouchBlankPosListener != null) {
                    mTouchBlankPosListener.onClick();
                    return true;
                } else
                    return false;
            }
        }

        return super.onTouchEvent(event);
    }

    /**
     * 设置GridView的空白区域的触摸事件
     *
     * @param listener
     */
    public void setOnBlankItemClickListener(OnBlankItemClickListener listener) {
        mTouchBlankPosListener = listener;
    }

    public interface OnBlankItemClickListener {
        void onClick();
    }
}
